$(function () {
    $("#jqGrid").Grid({
        url: '../standings/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '分类', name: 'typeName', index: 'type_name', width: 40},
			{label: '标题', name: 'title', index: 'title', width: 60},
			{
			    label: '主持时间', name: 'emceeTime', index: 'emcee_time', width: 60,
                formatter: function (value) {
                    return transDate(value);
                }},
			{label: '视频地址', name: 'url', index: 'url', width: 100},
            {label: '视频地址', name: 'thumbnailUrl', index: 'url', width: 100},
			{label: '说明', name: 'explain', index: 'explain', width: 100},
            {
                label: '是否可用', name: 'isShow', index: 'is_show', width: 40,
                formatter: function (value) {
                    return transIsNot(value);
                }
            }]
    });
    $('#desc').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: '500px', //高度
        minHeight: '200px',
        language: "zh_cn",
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',
        imageUploadParams: {id: "edit"},
        imagesLoadURL: '../sys/oss/queryAll'
    })
});

var ztree;

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "0",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
        imgName: '',
        visible: false,
		standings: {
            typeId: '',
            typeName: ''
        },
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.standings = {
                typeId: '',
				typeName: '',
                thumbnailUrl: '',
                url: ''

			};
            $('#desc').editable('setHTML', '');
            vm.getType();
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.standings = {
                typeId: '',
                typeName: ''

            };
            vm.getInfo(id)
            $('#desc').editable('setHTML', '');
            vm.getType();

		},
		saveOrUpdate: function (event) {
            let url = vm.standings.id == null ? "../standings/save" : "../standings/update";
            vm.standings.desc = $('#desc').editable('getHTML');
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.standings),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../standings/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../standings/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.standings = r.standings;
                    $('#desc').editable('setHTML', vm.standings.desc);
                    vm.getType();
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getType: function () {
            //加载分类树
            Ajax.request({
                url: "../standingstype/queryAll",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#categoryTree"), setting, r.list);
                    var node = ztree.getNodeByParam("id", vm.standings.typeId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.standings.typeName = node.name;
                    } else {
                        node = ztree.getNodeByParam("id", 1);
                        ztree.selectNode(node);
                        vm.standings.typeName = node.name;
                    }
                }
            });
        },
        categoryTree: function () {
            openWindow({
                title: "选择类型",
                area: ['300px', '450px'],
                content: jQuery("#categoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.standings.typeId = node[0].id;
                    vm.standings.typeName = node[0].name;

                    layer.close(index);
                }
            });
        },

        // handleView(name) {
        //     this.imgName = name;
        //     this.visible = true;
        // },
        // handleRemove(file) {
        //     // 从 upload 实例删除数据
        //     const fileList = this.uploadList;
        //     this.uploadList.splice(fileList.indexOf(file), 1);
        // },
        // handleSuccess(res, file) {
        //     // 因为上传过程为实例，这里模拟添加 url
        //     file.imgUrl = res.url;
        //     file.name = res.url;
        //     vm.uploadList.add(file);
        // },
        // handleBeforeUpload() {
        //     const check = this.uploadList.length < 5;
        //     if (!check) {
        //         this.$Notice.warning({
        //             title: '最多只能上传 5 张图片。'
        //         });
        //     }
        //     return check;
        // },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccessPicUrl: function (res, file) {
            vm.standings.thumbnailUrl = file.response.url;
        },
        handleSuccessPicUrla: function (res, file) {
            vm.standings.url = file.response.url;
        },
        // handleSuccessListPicUrl: function (res, file) {
        //     vm.standings.listPicUrl = file.response.url;
        // },
        eyeImagePicUrl: function () {
            var url = vm.standings.thumbnailUrl;
            eyeImage(url);
        },
        eyeImagePicUrla: function () {
            var url = vm.standings.url;
            eyeImage(url);
        },
        // eyeImageListPicUrl: function () {
        //     var url = vm.standings.listPicUrl;
        //     eyeImage(url);
        // },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        }
	}
});